<html>
<head>

<title>Groovy Goodness: GString as Writable</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: GString as Writable</h3>

<div class="post">
<p>The Groovy API has the interface <a href="http://groovy.codehaus.org/api/groovy/lang/Writable.html">Writable</a>. Classes that implement this interface are capable of writing their selves to a <code>java.io.Writer</code> object. The interface has one method <code>writeTo()</code> where the code is that writes the contents to a given <code>Writer</code> instance. Most implementations will also use the implementation of the <code>writeTo()</code> method in their <code>toString()</code> implementation.</p><p>The GString implementation in Groovy also implements the <code>Writable</code> interface. This means we can redirect the GString contents to some <code>Writer</code> instance if we want to. In the following code we use a <code>FileWriter</code> to write the contents of a GString to a file:</p><pre class="brush:groovy">def data = [
    new Expando(id: 1, user: 'mrhaki', country: 'The Netherlands'),
    new Expando(id: 2, user: 'hubert', country: 'The Netherlands'),
]

data.each { userData -&gt;
    new File("${userData.id}.txt").withWriter('UTF-8') { fileWriter -&gt;
        // Use writeTo method on GString to save
        // result in a file.
        "User $userData.user lives in $userData.country".writeTo(fileWriter)
    }
}


assert new File('1.txt').text == 'User mrhaki lives in The Netherlands'
assert new File('2.txt').text == 'User hubert lives in The Netherlands'
</pre><p>Code written with Groovy 2.2.2</p
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>